package model

import (
	"errors"
	"stock/global"

	"gorm.io/gorm"
)

type FileModel struct {
	Base
}

type MallFile struct {
	Id           uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	MerchantType uint8  `json:"merchant_type" gorm:"<-:false; column:merchant_type;"`
	MerchantId   uint   `json:"merchant_id" gorm:"<-:false; column:merchant_id;"`
	ProductId    uint   `json:"product_id" gorm:"<-:false; column:product_id;"`
	VideoCoverId uint   `json:"video_cover_id" gorm:"column:video_cover_id;"`
	FileType     uint8  `json:"file_type" gorm:"<-:false; column:file_type;"`
	UsageTypd    uint8  `json:"usage_typd" gorm:"<-:false; column:usage_typd;"`
	Path         string `json:"path" gorm:"column:path;"`
	FileLang     string `json:"lang" gorm:"<-:false; column:lang;"`
}

type RequestFile struct {
	MerchantType uint8
	MerchantId   uint
	ProductId    uint
	VideoCoverId uint
	FileType     uint8
	UsageTypd    []uint
	Lang         string
}

type ResponseFile struct {
	Video              []string `json:"video"`
	Factory            []string `json:"factory"`
	Honor              []string `json:"honor"`
	ProductImg         []string `json:"product_img"`
	PurchaseOrderCover string   `json:"purchase_order_cover"`
	Promotional        []string `json:"promotional"`
	DetailImg          []string `json:"detail_img"`
	Banner             []string `json:"banner"`
	Img                []string `json:"img"`
	CoverVideo         []*ResponseFileCoverVideo
	MainImg            string `json:"main_img"`
}

type ResponseFileCoverVideo struct {
	CoverVideo string `json:"cover_video"`
	VideoUrl   string `json:"video_url"`
}

func (m *FileModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_file"
}

func (m *FileModel) List(param RequestFile) (ResponseFile, error) {

	where := &MallFile{}
	if param.MerchantType != 0 {
		where.MerchantType = param.MerchantType
	}
	if param.MerchantId != 0 {
		where.MerchantId = param.MerchantId
	}
	if param.ProductId != 0 {
		where.ProductId = param.ProductId
	}
	if param.VideoCoverId != 0 {
		where.VideoCoverId = param.VideoCoverId
	}
	if param.FileType != 0 {
		where.FileType = param.FileType
	}
	if param.Lang != "" {
		where.FileLang = param.Lang
	}
	var resFileArr ResponseFile
	if len(param.UsageTypd) < 1 {
		return resFileArr, errors.New("parameter error")
	}

	var file []MallFile
	tx := global.DB.Debug().Where(where).Where("usage_typd IN ?", param.UsageTypd).Order("id DESC").Find(&file)
	if tx.Error != nil {
		return resFileArr, tx.Error
	}

	fileLen := len(file)
	if fileLen > 0 {
		for i := 0; i < fileLen; i++ {

			var resVideoCover ResponseFileCoverVideo
			var videoCover MallFile

			switch file[i].UsageTypd {
			case 1:
				resFileArr.Video = append(resFileArr.Video, file[i].Path)
				resVideoCover.VideoUrl = file[i].Path
				videoCover, _ = m.VideoCover(file[i].VideoCoverId, "id")
				if videoCover.Path != "" {
					resVideoCover.CoverVideo = videoCover.Path
				}
				resFileArr.CoverVideo = append(resFileArr.CoverVideo, &resVideoCover)

			case 2:
				resFileArr.Factory = append(resFileArr.Factory, file[i].Path)

			case 3:
				resFileArr.Honor = append(resFileArr.Honor, file[i].Path)

			case 4:
				resFileArr.ProductImg = append(resFileArr.ProductImg, file[i].Path)

			case 5:
				resFileArr.PurchaseOrderCover = file[i].Path

			case 6:
				resFileArr.Promotional = append(resFileArr.Promotional, file[i].Path)

			case 7:
				resFileArr.DetailImg = append(resFileArr.DetailImg, file[i].Path)

			case 8:
				resFileArr.Video = append(resFileArr.Video, file[i].Path)
				resVideoCover.VideoUrl = file[i].Path
				videoCover, _ = m.VideoCover(file[i].VideoCoverId, "id")
				if videoCover.Path != "" {
					resVideoCover.CoverVideo = videoCover.Path
				}
				resFileArr.CoverVideo = append(resFileArr.CoverVideo, &resVideoCover)

			case 9:
				resFileArr.ProductImg = append(resFileArr.ProductImg, file[i].Path)

			case 10:
				resFileArr.Img = append(resFileArr.Img, file[i].Path)

			case 11:
				resFileArr.Img = append(resFileArr.Img, file[i].Path)

			case 12:
				resFileArr.Banner = append(resFileArr.Banner, file[i].Path)

			case 13:
				resFileArr.MainImg = file[i].Path

			}
		}
	}
	return resFileArr, nil
}

func (m *FileModel) VideoCover(id uint, field string) (MallFile, error) {
	var reFile MallFile
	var tx *gorm.DB
	if id > 0 {
		tx = global.DB.Debug().Where("`"+field+"` = ?", id).Order("id DESC").Find(&reFile)
	}
	if tx.Error != nil {
		return reFile, tx.Error
	}
	return reFile, nil
}
